package cn.laixueit.service.impl;

import cn.laixueit.mapper.SubjectMapper;
import cn.laixueit.pojo.Subject;
import cn.laixueit.pojo.SubjectExample;
import cn.laixueit.service.SubjectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SubjectServiceImpl implements SubjectService {

    @Autowired
    private SubjectMapper subjectMapper;

//    @Override
//    public List<Subject> all() {
//        return subjectMapper.selectByExample(null);
//    }

    public PageInfo<Subject> all(Integer startpage, Integer pagesize) {
        SubjectExample example = new SubjectExample();
        SubjectExample.Criteria criteria = example.createCriteria();
        criteria.andIsdeleteEqualTo(0);
        PageHelper.startPage(startpage, pagesize);
        List<Subject> subjectList = subjectMapper.selectByExample(example);
        PageInfo<Subject> pageInfo = new PageInfo<>(subjectList);
        return pageInfo;
    }

    public List<Subject> getisdeleteList() {
        SubjectExample example = new SubjectExample();
        SubjectExample.Criteria criteria = example.createCriteria();
        criteria.andIsdeleteEqualTo(1);
        return subjectMapper.selectByExample(example);
    }

    @Override
    public void edit(Subject subject) {
        Subject sub = subjectMapper.selectByPrimaryKey(subject.getId());
        subject.setIsdelete(sub.getIsdelete());
        subjectMapper.updateByPrimaryKey(subject);
    }

    @Override
    public Subject getById(String id) {
        Subject subject = subjectMapper.selectByPrimaryKey(id);
        return subject;
    }

    @Override
    public void update(String id,String isdelete) {
//        subjectMapper.deleteByPrimaryKey(id);
        Subject subject = subjectMapper.selectByPrimaryKey(id);
        if(subject!=null){
            subject.setIsdelete(Integer.parseInt(isdelete));
            subjectMapper.updateByPrimaryKey(subject);
        }
    }
}
